/**
 * Created by billy on 2017/4/25.
 */
var g = require("nodeLib");
var resultObj = require("../work");
var classObj = require("../type");
var lasterItemObj = {};
var _sql;
var _file = g.data.file.get("user");
var arr = [];
module.exports = class {
	constructor()
	{

		_file.add(__projpath("./module/sql/user"));
		_sql = g.data.manager.getManager("mysql");
		this.add("run", this.run);
// 		this.add("addData", this.addData);
		this.add("addForm", this.addForm);
		this.add("insert", this.insertFormClass);
	}

	run($data, $success, $error, $client)
	{
//js生成量表分类脚本
		for (var key in resultObj)
		{
			var itemList = resultObj[key]
			for (var i = 0; i < itemList.length; i++)
			{
				var keys = itemList[i].class1;
				if (Object.keys(lasterItemObj).indexOf(keys) < 0)
				{

					lasterItemObj[keys] = [];
				}

				if (itemList[i].class2)
				{
					if (lasterItemObj[keys].indexOf(itemList[i].class2) < 0)
					{
						lasterItemObj[keys].push(itemList[i].class2)
					}
				}

			}
		}

		g.fs.writeFileSync(__projpath("type.json"), JSON.stringify(lasterItemObj));
		trace("ALL DONE!");
		process.exit();
	}

	addData($data, $success, $error, $client)
	{
//导入静态表的路由
		var keysList = Object.keys(classObj);
		var sqlStr = ''
		for (var i = 0; i < keysList.length; i++)
		{
			sqlStr += _file.get("selectData.sql", {name: keysList[i]}) + ';';
		}

		_sql.query(sqlStr, ($list) => {

			var list = [];
			for (var k = 0; k < $list.length; k++)
			{
				list = list.concat($list[k]);
			}

// 			$success(list);
			var parentId, name;
			var _sqlStr = '';
			var id = 45;
			for (var key in classObj)
			{
				for (var j = 0; j < list.length; j++)
				{
					if (key == list[j].name)
					{
// 						trace()
						parentId = list[j].id;
						for (var a = 0; a < classObj[key].length; a++)
						{
							name = classObj[key][a];

// 							trace(parentId)
							_sqlStr += _file.get("insertData.sql",
								{
									name: name,
									type: 3,
									parentId: parentId

								}
							)
						}
					}

				}
			}
// 			trace(_sqlStr);
			_sql.query(_sqlStr, ($list) => {
				$success("")
			})

		})
	}

	insertFormClass($data, $success, $error, $client)
	{
		var allList = require("../allClass");
		var classList = [];
		var classHash = {};
		var list = [];
		for (var formListObj of allList)
		{
			for (var item of formListObj.list)
			{
				var class1 = item.class1;
				var class2 = item.class2;
				let obj = {};
				obj.class1 = item.class1;
				obj.class2 = item.class2;
				list.push(obj);
				createClass(classList, classHash, class1, class2);
			}
		}

		var sqlStr = "";
		for (var i = 0; i < classList.length; i++)
		{
			var className_type = classList[i];
			var classObj = classHash[className_type];
			classList[i] = classObj;
// 			sqlStr += _file.get("insertData.sql", classObj);
		}
		classList.sort(function (a, b) {
			if (a.parentId != b.parentId)
			{
				return a.parentId - b.parentId;
			}
			return a.id - b.id;
		});

		for (var i = 0; i < classList.length; i++)
		{
			var classObj = classList[i];
			classObj.id = i + 1;
		}

		for (var i = 0; i < classList.length; i++)
		{
			var classObj = classList[i];
			if (classObj.type == 2)
			{
				var parentClassObj = classHash[classObj.parent + "_1"];
				classObj.parentId = parentClassObj.id;
			}
			sqlStr += _file.get("insertData.sql", classObj);
		}

		_sql.query(sqlStr, ($list) => {
//			g.fs.writeFileSync(__projpath("list.js"), JSON.stringify(list));
			trace("DONE~~~~~")
			process.exit();
		})
	}

	addForm($data, $success, $error, $client)
	{
		var optionsType;
		var _sqlStr = _file.get("insertFormData.sql");
		_sql.query(_sqlStr, ($list) => {
// 		trace($list);
			arr = arr.concat($list);
// 			for(var key in resultObj){
// 			if(key=='mood'){
// 				optionsType = 1
// 			}else{
// 				optionsType = 0;
// 			}
// 			var list = resultObj[key];
//
//
//
//
// 		}
// 		trace(arr)

		})
		$success(arr)

	}
}

function createClass($arr, $hash, $class1, $class2)
{
	if ($arr.indexOf($class1 + "_1") < 0)
	{
		$arr.push($class1 + "_1");
		var id = $arr.length;
		$hash[$class1 + "_1"] = {
			id: id,
			name: $class1,
			parentId: 0,
			parent: "",
			type: 1
		};
	}

	if ($class2)
	{
		var parentId = $hash[$class1 + "_1"].id;
		if ($arr.indexOf($class2 + "_2") < 0)
		{
			$arr.push($class2 + "_2");
			var id = $arr.length;
			$hash[$class2 + "_2"] = {
				id: id,
				name: $class2,
				parentId: parentId,
				parent: $class1,
				type: 2
			};
		}
	}
}
